{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Testing the FastAPI REST API\n",
    "\n",
    "This notebook shows a quick example of sending batches of texts to your REST API to process them with spaCy. Make sure the server is running on the given host and port via `python -m spacy project serve`. You'll also need a test file `assets/data.jsonl` – run `python -m spacy project assets` to download the example data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import srsly\n",
    "import requests\n",
    "from pprint import pprint\n",
    "import random\n",
    "\n",
    "# For simplicity, we load the project.yml to read off the host and port from \n",
    "# there so we don't have to hard-code it in the notebook\n",
    "project_yml = srsly.read_yaml(\"../project.yml\")\n",
    "API_URL = f\"http://{project_yml['vars']['host']}:{project_yml['vars']['port']}\"\n",
    "print(\"API URL:\", API_URL)\n",
    "\n",
    "# We also load the data we want to process via the API. Each record will have \n",
    "# a key \"text\" containing the text, which is also expected bx the API\n",
    "DATA = list(srsly.read_jsonl(\"../assets/data.jsonl\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. GET request: list all models\n",
    "\n",
    "The `/models` endpoint gives us a list of all available loaded models."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.get(f\"{API_URL}/models\")\n",
    "pprint(r.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. POST request: process batches of text\n",
    "\n",
    "The `/process` endpoint lets process batches of articles with a given pipeline."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch = random.sample(DATA, 5)\n",
    "payload = {\"articles\": batch, \"model\": \"en_core_web_sm\"}\n",
    "r = requests.post(f\"{API_URL}/process/\", json=payload)\n",
    "pprint(r.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we submit a request with the name of an invalid package that isn't loaded, the API will return an error and list the available names."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "payload = {\"articles\": batch, \"model\": \"invalid_name\"}\n",
    "r = requests.post(f\"{API_URL}/process/\", json=payload)\n",
    "pprint(r.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".env37",
   "language": "python",
   "name": ".env37"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
